package com.web.dao.impl;

import com.web.dao.StudentDAO;
import com.web.entity.Student;
import com.web.util.JDBCUtils;

import java.util.List;

public class StudentDAOImpl implements StudentDAO {
    @Override
    public List<Student> selectAllStudents() {
        return JDBCUtils.query(Student.class,"select * from student");
    }

    @Override
    public List<Student> searchStudentsByName(String keyword) {
        return JDBCUtils.query(Student.class,"select*from student where name like ?" ,"%"+keyword+"%");
    }

    @Override
    public List<Student> findStudentById(int id) {
        return JDBCUtils.query(Student.class,"select *from student where id=?",id);
    }

    @Override
    public int insertStudent(Student student) {
        return JDBCUtils.update("insert into student (name, age) VALUES (?, ?)", student.getName(), student.getAge());
    }

    @Override
    public int updateStudent(Student student) {
        return JDBCUtils.update("UPDATE student SET name = ?, age = ? WHERE id = ?", student.getName(), student.getAge(), student.getId());
    }

    @Override
    public int deleteStudent(int id)
    {
        return JDBCUtils.update("DELETE FROM student WHERE id = ?", id);
    }

}